starter.controller('bookingDateController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page, localStore,$location,$ionicHistory,$ionicGesture,sessionStore) {
		$scope.line = $state.params.line || sessionStore.getObject('booking.line') || {};
		$scope.order = $state.params.order  || sessionStore.getObject('booking.order') || {};
		
		if(!$scope.order || !$scope.line || !$scope.line.stops 
				|| !$scope.order.plan || !$scope.order.plan.planId){
			return false;
		}
		
		$scope.order.rideDates = [];
		
		
		var calendarLis = "";
		var calendarNum = 1;
		$scope.initCalendar = function(startY,startM,endY,endM,bookableDays,defaultDays){
				calendarNum = 1;
				calendarLis = "";
				jsonstr = "[]";
				jsonarray = eval('(' + jsonstr + ')');
				$("#dayStr").val("");
				
				var drawY = startY;
				var drawM = startM;
				
				while(drawY < endY || (drawY == endY && drawM <= endM)){
					drawCld(drawY,(drawM-1));
					drawY = drawM < 12 ? drawY : (drawY + 1);
					drawM = drawM < 12 ? (drawM + 1) : 1;
				}
				
				for (var i = 0; i < jsonarray.length; i++) {
					var className = "undays";//
					var element = jsonarray[i];
					var seating = null;
				   
				   if(i>7 && element.solar == ""){
					   continue;
				   }
					
				   if(calendarNum==1||calendarNum==8){
					   calendarNum = 1;
					   calendarLis += '<tr>';  
				   }
				   
				   if(i<8 && element.solar == ""){
					   calendarLis += '<td></td>';
					   calendarNum++;
					   continue;
				   }
			      
				   $(bookableDays).each(function(i,date){ 
						if(date.date == element.day){
							seating = date.seating || 0;
							if(seating != 0 && seating != '0'){
								className = "days";
							}
							return false;
						}
				   });
				   
				   $(defaultDays).each(function(i,date){ 
						if(date.date == element.day){
							className += ' ' + 'nac';
							return false;
						}
				   });
				   
				   
				   calendarLis += '<td class="'+className+'"><input type="hidden" value="'+element.day+'"><font>'+element.solar+'</font><br/><font>'+element.lunar+'</font>' +
				   (seating != null ? ('<span>' + seating + '</span>') : '')
				   +'</td>';
			       
				   if(calendarNum==7){
					   calendarLis += '</tr>'; 
				   }
				   calendarNum++;
				}
				//补齐日历格
		        if(calendarNum < 8){
		        	for (var i = calendarNum; i < 8; i++) {
		        		calendarLis += '<td></td>';
					}
		        }
		        calendarLis += '</tr>';
				
		      	//console.log(calendarLis);
		        $("#order_calendar").find('tr').eq(0).nextAll().remove();
				$("#order_calendar").find('tr').eq(0).after(calendarLis);
		};
		
		$(document).off('click','#order_calendar .days');
		$(document).on('click','#order_calendar .days',function(e){
			e.preventDefault();	
			if($(this).hasClass("nac")){
				$(this).toggleClass("nac");
			}else{
				$(this).addClass("nac");
			}
		});
		
		
		$scope.today = new Date();
		$scope.currentY = $scope.today.getFullYear();
		$scope.currentM = $scope.today.getMonth() + 1;
		if($scope.currentM == 12){
			$scope.lastY = $scope.currentY + 1;
			$scope.lastM = 1;
		}else{
			$scope.lastY = $scope.currentY;
			$scope.lastM = $scope.currentM + 1;
		}
		
		
		Page.ajaxPost('linePlan/canBookDays.do',{planId : $scope.order.plan.planId},function(resp){
			if(resp.data.length > 0){
				var lDate = resp.data[resp.data.length - 1].date;
				if(lDate){
					var lastDate = new Date(lDate);
					var lY = lastDate.getFullYear();
					var lM = lastDate.getMonth() + 1;
					$scope.lastY = lY > $scope.lastY ? lY : $scope.lastY;
					$scope.lastM = lY > $scope.lastY ? lM : (lM > $scope.lastM ? lM : $scope.lastM);
				}
			}
			$scope.initCalendar($scope.currentY,$scope.currentM,$scope.lastY,$scope.lastM, resp.data,$scope.order.plan.planStatus == '0' ? resp.data : []);
		},function(resp){
			$scope.initCalendar($scope.currentY,$scope.currentM,$scope.lastY,$scope.lastM, [],[]);
		});
		
		if($scope.order.plan.planStatus == '0'){//待开-新线招募
			$(document).off('click','#order_calendar .days');
		}
		
		

		
		
		$scope.next = function(){
			$scope.order.rideDates = [];
			$(".nac").each(function(i){ 
				$scope.order.rideDates.push($(this).find('input').eq(0).val());
			});
			if($scope.order.rideDates.length == 0){
				Page.alert("请选择乘车日期!");
				return;
			}
			sessionStore.setObject('booking.order', $scope.order);
			$state.go('booking_passenger', {line: $scope.line, order: $scope.order}, {reload:true});
		};
});